package com.css.fxfzaqbz.modules.data.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess2.entity.UrgencysettleData;
import com.css.fxfzaqbz.modules.data.entity.AidedAnalyse;
import com.css.fxfzaqbz.modules.data.repository.AidedAnalyseRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class AidedAnalyseRepositoryImpl implements AidedAnalyseRepository {
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Override
    public void saveAidedAnalyse(AidedAnalyse aidedAnalyse) {
        String sql="insert into sys_aided_analyse (id,ia_id,title,status,generated_time,salesman,data_message," +
                "picture_id,content,del_flag,create_user,create_time,task_name,zdmb) values " +
                "(:id,:iaId,:title,:status,:generatedTime,:salesman,:dataMessage,:pictureId," +
                ":content,:delFlag,:createUser,:createTime,:taskName,:zdmb)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(aidedAnalyse);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
    }

    @Override
    public AidedAnalyse getAidedAnalyseById(String id) {
        String sql = "select * from sys_aided_analyse where id = '" + id + "';";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(AidedAnalyse.class));
    }

    @Override
    public void delAidedAnalyse(List<String> ids, Timestamp timestamp, String userID) {
        String sql = "update sys_aided_analyse set del_flag = '" + YNEnum.Y + "' , update_time = '" + timestamp + "' ,update_user = '" + userID + "'" + " where id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }

    @Override
    public AidedAnalyse updateAidedAnalyse(AidedAnalyse aidedAnalyse) {
        String sql = "update sys_aided_analyse set" +
                " picture_id = :pictureId, " +
                " title = :title," +
                " update_time = :updateTime, " +
                " update_user = :updateUser " +
                " where id = '" + aidedAnalyse.getId() + "'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(aidedAnalyse);
        namedParameterJdbcTemplate.update(sql, source);
        return aidedAnalyse;
    }

    @Override
    public List<AidedAnalyse> getAidedAnalyseByIaId(String iaId) {
        String sql = "select * from sys_aided_analyse where ia_id = '" + iaId + "' and del_flag = '"+YNEnum.N+"' and status = '"+YNEnum.N+"' order by generated_time desc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(AidedAnalyse.class));
    }

    @Override
    public List<AidedAnalyse> getByIaId(String iaId) {
        String sql = "select * from sys_aided_analyse where ia_id = '" + iaId + "' and del_flag = '"+YNEnum.N+"' and status != '"+YNEnum.N+"' order by content desc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(AidedAnalyse.class));
    }

    @Override
    public void saveDescription(List<String> ids, String description) {
        String sql = "update sys_aided_analyse set" +
                " description = '"+description+"' " +
                " where id in (:ids) ";
        HashMap<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        namedParameterJdbcTemplate.update(sql,map);
    }

    @Override
    public List<AidedAnalyse> check(AidedAnalyse aidedAnalyse) {
        String sql = "select * from sys_aided_analyse where ia_id = '" + aidedAnalyse.getIaId() + "' and del_flag = '"+YNEnum.N+"' and title = '"+aidedAnalyse.getTitle()+"' and content ='"+aidedAnalyse.getContent()+"'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(AidedAnalyse.class));
    }

    @Override
    public List<AidedAnalyse> getByEntity(AidedAnalyse aidedAnalyse) {
        String sql = "select * from sys_aided_analyse where ia_id = '" + aidedAnalyse.getIaId() + "' and del_flag = '"+YNEnum.N+"' and content = '"+aidedAnalyse.getContent()+"' and status = '"+YNEnum.Y+"'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(AidedAnalyse.class));
    }

    @Override
    public void submitResult(String id, String description) {
        String sql = "update sys_aided_analyse set" +
                " description = '"+description+"', " +
                " status = '"+YNEnum.Y+"' " +
                " where id = '" + id + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void removeResult(AidedAnalyse aidedAnalyse) {

        String sql = "update sys_aided_analyse set" +
                " description = '"+aidedAnalyse.getDescription()+"', " +
                " submiter = '"+aidedAnalyse.getSubmiter()+"', " +
                " submit_phone = '"+aidedAnalyse.getSubmitPhone()+"', " +
                " status = '"+YNEnum.N+"' " +
                " where id = '" + aidedAnalyse.getId() + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void finalSubmission(List<String> ids,String userName,String mobilePhone,Timestamp currentTimestamp) {
        String sql = "update sys_aided_analyse set" +
                " status = '"+2+"', " +
                " submiter = '"+userName+"', " +
                " update_time = '"+currentTimestamp+"', " +
                " submit_phone = '"+mobilePhone+"', " +
                " submit_time = '"+currentTimestamp+"' " +
                " where id in (:ids) ";
        HashMap<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        namedParameterJdbcTemplate.update(sql,map);
    }

    @Override
    public List<AidedAnalyse> getFzByIaId(String iaId) {
        String sql = "select * from sys_aided_analyse where ia_id = '" + iaId + "' and del_flag = '"+YNEnum.N+"' and status = '"+2+"' order by update_time desc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(AidedAnalyse.class));
    }

    @Override
    public List<AidedAnalyse> getAnalysisByIaId(String iaId) {
        String sql = "select * from sys_aided_analyse where ia_id = '" + iaId + "' and del_flag = '"+YNEnum.N+"'and status = '"+YNEnum.Y+"' ";
        try {
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(AidedAnalyse.class));
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public List<UrgencysettleData> UniquenessCheck(String iaId) {
        String sql = "select * from sys_urgencysettle_data where ud_relation = '" + iaId + "' and del_flag = '"+YNEnum.N+"' and status = '2' or status= '1'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(UrgencysettleData.class));

    }


}
